package com.guiassysdesigners.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.CascadeType;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * Essa classe representa as sugestões que os usuários podem enviar para o
 * administrador do sistema. Com base nessas sugestões os desenvolvedores podem
 * tomar decisões para melhor a aplicação.
 * 
 * @author <a href="mailto:guiassys@gmail.com">Guilherme Assis</a>
 * 
 */
@Entity
@Table(name = "app_suggestion")
@Indexed
@XmlRootElement
public class Suggestion extends BaseObject implements Serializable {
	private static final long serialVersionUID = 5350308206785688840L;

	private Long id;
	private String title; // required
	private String description; // required
	private User user; // required

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @DocumentId
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

    @Column(nullable = false, length = 100, unique = false)
    @Field
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(nullable = false, length = 500, unique = false)
    @Field
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}


	/**
	 * {@inheritDoc}
	 */
	public String toString() {
		ToStringBuilder sb = new ToStringBuilder(this,
				ToStringStyle.DEFAULT_STYLE);
		sb.append("title", this.title);
		sb.append("description", this.description);
		return sb.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Suggestion)) {
            return false;
        }

        final Suggestion suggestion = (Suggestion) o;

        return !(title != null ? !title.equals(suggestion.getTitle()) : suggestion.getTitle() != null);
	}

	/**
	 * {@inheritDoc}
	 */
	public int hashCode() {
		return (title != null ? title.hashCode() : 0);
	}

    @OneToOne(fetch = FetchType.EAGER)
    @JoinTable(
            name = "suggestion_user",
            joinColumns = { @JoinColumn(name = "suggestion_id") },
            inverseJoinColumns = @JoinColumn(name = "user_id")
    )
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	

}
